package com.example.chouti;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.example.chouti.fragment.FirstFragment;
import com.example.chouti.fragment.SecondFragment;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    DrawerLayout mDrawerLayout;
    ActionBarDrawerToggle mToggle;
    LinearLayout mRightContentll;
    NavigationView mNavv;
    Fragment mFragment;
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment firstFragment;
    Class fragmentClass;
    LinearLayout card1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mRightContentll = findViewById(R.id.rightContent);
        mNavv = findViewById(R.id.nv);
        initNv(mNavv);
        card1 = (LinearLayout)findViewById(R.id.cardContent_1);
        card1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            Intent intent1 = new Intent(MainActivity.this,Card1Activity.class);
            intent1.putExtra("cardTitle","firstCard");
            startActivity(intent1);
            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mToggle.onOptionsItemSelected(item))
            return true;
        else
            return super.onOptionsItemSelected(item);
    }

    private void initNv(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            mRightContentll.setVisibility(View.INVISIBLE);
            nvItemSelect(menuItem);
            return true;
            }
        });
    }
    private void nvItemSelect(MenuItem item){
        switch (item.getItemId()){
            case R.id.item1:{
                mRightContentll.setVisibility(View.VISIBLE);
                fragmentClass = FirstFragment.class;
                break;
            }
            case R.id.item2:{
                fragmentClass = SecondFragment.class;
            }
        }
        try {
            mFragment = (Fragment)fragmentClass.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        fragmentManager.beginTransaction().replace(R.id.rightTitle,mFragment).commit();
        item.setChecked(true);
        setTitle(item.getTitle());
        mDrawerLayout.closeDrawers();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.firstCard: {
                Intent intent = new Intent(this,Card1Activity.class);
                intent.putExtra("cardTitle","firstCard");
                startActivity(intent);
                break;
            }
        }
    }
}
